package com.hmy.contract.manager.worklfow;


import cn.hutool.json.JSONUtil;
import com.hmy.ccp.common.result.FeignResult;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.workflow.api.bo.request.process.RevokeProcessRequest;
import com.hmy.workflow.api.bo.request.process.StartProcessInstanceRequest;
import com.hmy.workflow.api.bo.response.process.StartProcessInstanceResponse;
import com.hmy.workflow.api.client.process.FlowProcessClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Component
@Slf4j
public class FlowProcessClientManager {


    @Resource
    private FlowProcessClient flowProcessClient;


    /**
     * 创建流程
     *
     * @param startProcessInstanceRequest startProcessInstanceRequest
     */
    public StartProcessInstanceResponse start(StartProcessInstanceRequest startProcessInstanceRequest) {
        FeignResult<StartProcessInstanceResponse> response = null;
        try {
            log.info("FlowProcessClientRpc.start params:{}", JSONUtil.toJsonPrettyStr(startProcessInstanceRequest));
            response = flowProcessClient.start(startProcessInstanceRequest);
            if (Objects.isNull(response)) {
                throw new HMYRuntimeException(502000005);
            }
            if (response.isFailed()) {
                throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
            }
            log.info("FlowProcessClientRpc.start response:{}", JSONUtil.toJsonPrettyStr(response));
        } catch (Throwable e) {
            log.error("FlowProcessClientRpc.start exception", e);
            throw new HMYRuntimeException(502000005);
        }
        return response.getData();
    }


    /**
     * 撤销操作
     *
     * @param request 撤销请求
     * @return true/false
     */
    public Boolean revoke(RevokeProcessRequest request) {
        log.info("FlowProcessClientRpc.revoke params:{}", request);
        FeignResult<Boolean> response;
        try {
            response = flowProcessClient.revoke(request);
        } catch (Exception e) {
            log.error("FlowProcessClientRpc.revoke exception:", e);
            throw new HMYRuntimeException(502000005);
        }
        if (response.isFailed()) {
            log.error("FlowProcessClientRpc.revoke fail {},{}", response.getMsgCode(), response.getErrorText());
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("FlowProcessClientRpc.revoke end:{}", response);
        return response.getData();
    }

    /**
     * 删除工作流
     *
     * @param processInstanceId 流程实例id
     * @return true/false
     */
    public Boolean remove(String processInstanceId) {
        log.info("FlowProcessClientRpc.remove params:{}", processInstanceId);
        FeignResult<Boolean> response;
        try {
            response = flowProcessClient.remove(processInstanceId);
        } catch (Exception e) {
            log.error("FlowProcessClientRpc.remove exception:", e);
            throw new HMYRuntimeException(502000005);
        }
        if (response.isFailed()) {
            log.error("FlowProcessClientRpc.remove fail {},{}", response.getMsgCode(), response.getErrorText());
            throw new HMYRuntimeException(response.getMsgCode(), response.getErrorText());
        }
        log.info("FlowProcessClientRpc.remove end:{}", response);
        return response.getData();
    }

}